<template>
  <el-dialog :title="title" :visible="visible" append-to-body destroy-on-close @close="close" @open="open">
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" placeholder="2-10字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" placeholder="2-10字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="formData.managerId" placeholder="请选择部门负责人" style="width: 100%;">
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" type="textarea" placeholder="1-100字符" />
      </el-form-item>

    </el-form>
    <div style="display: flex;justify-content: center;">
      <el-button type="primary" @click="handleSubmit">确定</el-button>
      <el-button @click="close">取消</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { getDepartmentManager, getDepartmentDetail, addDepartment, editDepartment } from '@/api/department'
export default {
  name: 'DialogComp',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      default: '-1'
    },
    // type: {
    //   type: String,
    //   default: ''
    // },
    depts: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      formData: {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' },
          { validator: (rule, value, callback) => {
            if (this.formData.id) {
              if (this.depts.filter(item => item.name !== this.formData.name).some(item => item.name === value)) {
                callback(new Error('部门名称已存在'))
              } else {
                callback()
              }
            } else {
              if (this.depts.some(item => item.name === value)) {
                callback(new Error('部门名称已存在'))
              } else {
                callback()
              }
            }
            // if (this.depts.some(item => item.name === value) && this.xx.name !== value) {
            //   callback(new Error('部门名称已存在'))
            // } else {
            //   callback()
            // }
          }, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' },
          { validator: (rule, value, callback) => {
            if (this.formData.id) {
              if (this.depts.filter(item => item.code !== this.formData.code).some(item => item.code === value)) {
                callback(new Error('部门编码已存在'))
              } else {
                callback()
              }
            } else {
              if (this.depts.some(item => item.code === value)) {
                callback(new Error('部门编码已存在'))
              } else {
                callback()
              }
            }
            // if (this.depts.some(item => item.code === value) && this.xx.code !== value) {
            //   callback(new Error('部门编码已存在'))
            // } else {
            //   callback()
            // }
          }, trigger: 'blur' }
        ],
        managerId: [
          { required: true, message: '请输入部门负责人', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur' }
        ]
      },
      managerList: []
    }
  },
  computed: {
    title() {
      // return this.type === 'add' ? '添加子部门' : '编辑部门'
      return this.formData.id ? '编辑部门' : '添加部门'
    }
  },
  // watch: {
  //   id(newVal) {
  //     if (this.type === 'add') {
  //       this.formData.pid = newVal
  //     }
  //   }
  // },
  created() {
    this.getDepartmentManager()
  },
  methods: {
    close() {
      this.$emit('update:visible', false)
      this.$refs.formRef.resetFields()
      this.formData = {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: ''
      }
    },
    open() {
      // if (this.type === 'edit') {
      //   this.getDepartmentDetail()
      // }
      // if (this.type === 'add') {
      //   this.formData.pid = this.id
      // }
    },
    handleSubmit() {
      this.$refs.formRef.validate(async(valid) => {
        if (valid) {
          try {
            if (!this.formData.id) {
              await addDepartment(this.formData)
              this.$message.success('添加成功')
              this.close()
            } else {
              await editDepartment(this.formData)
              this.$message.success('编辑成功')
              this.close()
            }
            this.$emit('refresh')
          } catch (error) {
            // this.$message.error('操作失败')
            console.dir(error)
          }
        }
      })
    },
    async getDepartmentManager() {
      const res = await getDepartmentManager()
      this.managerList = res
    },
    async getDepartmentDetail() {
      const res = await getDepartmentDetail(this.id)
      this.formData = res
    }
  }
}
</script>
<style scoped>
</style>

